﻿
using TbShop.Business.Components;
using TbShop.Entity;
using TbShop.Web.Helpers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TbShop.Web.Areas.Admin.Controllers
{
    public class RoleController : BaseController
    {
        private AspNetRoleBC roleBC = new AspNetRoleBC();
        /// <summary>
        /// 角色管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public string GetRoleList(int pageSize = 0, int pageNumber = 0)
        {
            var data = roleBC.GetPagedList(pageNumber, pageSize);
            var meta = data.GetMetaData();
            var counts = meta.TotalItemCount;
            return JsonConvert.SerializeObject(new { total = counts, rows = data });
        }
        /// <summary>
        /// 修改角色页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Edit(string id)
        {
            var model = new AspNetRoles();
            if (!string.IsNullOrEmpty(id))
            {
                model = roleBC.Get(id);
            }
            return View(model);
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(AspNetRoles model)
        {
            if (ModelState.IsValid)
            {
                if (!roleBC.CheckName(model.Id, model.Name))
                {
                    SendMessage("角色名称不能重复", Constants.MessageType.Warning);
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(model.Id))
                    {
                        model.VersionNumber = 1;
                        model.CreatedDate = DateTime.Now;
                        model.CreatedBy = User.Identity.Name;
                    }
                    else
                    {
                        model.VersionNumber++;
                    }
                    model.UpdatedDate = DateTime.Now;
                    model.UpdatedBy = User.Identity.Name;
                    var res = roleBC.Save(model);
                    if (res != null)
                    {
                        OperationLogHelper.SaveOperationLog(AdminId, !string.IsNullOrEmpty(model.Id) ? "添加" : "修改", "角色", "", res.Id.ToString());
                        SendMessage("保存成功", Constants.MessageType.Success);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        SendMessage("保存失败", Constants.MessageType.Success);
                    }
                }
            }
            return View(model);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(string id, int versionNumber)
        {
            var result = roleBC.Remove(id, versionNumber);
            if (result != null)
            {
                OperationLogHelper.SaveOperationLog(AdminId, "删除", "角色", "", id.ToString());
                SendMessage("删除成功", Constants.MessageType.Success);
            }
            else
            {
                SendMessage("删除失败", Constants.MessageType.Warning);
            }
            return RedirectToAction("Index");
        }


    }
}